package com.shenyue.nbtownlet.adapter;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.shenyue.nbtownlet.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private List<BaseFragment> mListViews = new ArrayList<>();
    private BaseFragment currentFragment;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setList(List<BaseFragment> mListViews) {
        this.mListViews = mListViews;
    }

    @Override
    public BaseFragment getItem(int position) {
        return mListViews.get(position);
    }

    @Override
    public int getCount() {
        return mListViews.size();
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (getCurrentFragment() != object) {
            currentFragment = ((BaseFragment) object);
        }
        super.setPrimaryItem(container, position, object);
    }

    public BaseFragment getCurrentFragment() {
        return currentFragment;
    }

    public void addFragment(BaseFragment fragment) {
        mListViews.add(fragment);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return getPageItemTitle(position);
    }

    public String getPageItemTitle(int position) {
        return mListViews.get(position).getName();
    }
}
